package com.example.campus;

import java.io.IOException;
import java.net.MalformedURLException;


import com.news.m.Net;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.Button;
import android.widget.EditText;

public class NetWebController implements OnClickListener {
	EditText xq;
	View v;
	EditText xn;
	Button go;
	WebView web;
	Context context;
	String resp;
	public static final int KEBIAO = 0;
	public static final int GRADES = 1;

	int type;

	public View getView() {
		return v;
	}

	public NetWebController(Context context, int type) {
		this.context = context;
		this.type = type;

	}

	public void init() {
		v = LayoutInflater.from(context).inflate(R.layout.kebiao, null);
		xq = (EditText) v.findViewById(R.id.xq);
		xn = (EditText) v.findViewById(R.id.xn);
		xq.setInputType(InputType.TYPE_NULL);
		xn.setInputType(InputType.TYPE_NULL);
		go = (Button) v.findViewById(R.id.go);
		xq.setOnClickListener(this);
		xn.setOnClickListener(this);
		go.setOnClickListener(this);
		web = (WebView) v.findViewById(R.id.web);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.xn) {
			final String[] info = new String[] { "2012-2013", "2013-2014",
					"2014-2015", "2015-2016" };
			new AlertDialog.Builder(context)
					.setTitle("请选择")
					.setIcon(android.R.drawable.ic_dialog_info)
					.setSingleChoiceItems(info, 0,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									xn.setText(info[which]);
									dialog.dismiss();
								}
							}).setNegativeButton("确认", null).show();
		} else if (v.getId() == R.id.xq) {
			final String[] info = new String[] { "1", "2" };
			new AlertDialog.Builder(context)
					.setTitle("请选择")
					.setIcon(android.R.drawable.ic_dialog_info)
					.setSingleChoiceItems(info, 0,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									xq.setText(info[which]);
									dialog.dismiss();
								}
							}).setNegativeButton("确认", null).show();
		}

		else if (v.getId() == R.id.go) {
			resp = null;
			new Thread() {
				public void run() {
					try {
						if (type == KEBIAO)
							resp = Net.resp(context.getAssets(), xq
									.getText().toString(), xn.getText()
									.toString());
						else {
							resp = Net.doSearchGrades(context
									.getAssets(), xq.getText().toString(), xn
									.getText().toString());
						}
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						Log.v("tag", "enull" + e.getMessage());
						;
					}

					Handler handler = new Handler(Looper.getMainLooper());
					handler.post(new Runnable() {

						@Override
						public void run() {
							if (resp == null) {

								return;
							}
							// TODO Auto-generated method stub
							if (type == KEBIAO) {

								web.getSettings().setJavaScriptEnabled(true);

								web.getSettings().setSupportZoom(true);

								web.getSettings().setBuiltInZoomControls(true);

								web.getSettings().setUseWideViewPort(true);

								web.getSettings().setLayoutAlgorithm(
										LayoutAlgorithm.SINGLE_COLUMN);
								web.getSettings().setLoadWithOverviewMode(true);
								web.getSettings().setUseWideViewPort(true);
								web.getSettings().setLoadWithOverviewMode(true);
							}
							web.loadDataWithBaseURL(null, resp, "text/html",
									"utf-8", null);
						}
					});
				}
			}.start();

		}
	}
}
